package com.ruoyi.wemedia.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.wemedia.domain.bo.NewsBo;
import com.ruoyi.wemedia.domain.vo.NewsVo;
import com.ruoyi.wemedia.domain.News;
import com.ruoyi.wemedia.mapper.NewsMapper;
import com.ruoyi.wemedia.service.INewsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 文章列表Service业务层处理
 *
 * @author xiaoqiang
 * @date 2022-10-15
 */
@RequiredArgsConstructor
@Service
public class NewsServiceImpl implements INewsService {

    private final NewsMapper baseMapper;

    /**
     * 查询文章列表
     */
    @Override
    public NewsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询文章列表列表
     */
    @Override
    public TableDataInfo<NewsVo> queryPageList(NewsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<News> lqw = buildQueryWrapper(bo);
        Page<NewsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文章列表列表
     */
    @Override
    public List<NewsVo> queryList(NewsBo bo) {
        LambdaQueryWrapper<News> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<News> buildQueryWrapper(NewsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<News> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, News::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), News::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), News::getContent, bo.getContent());
        lqw.eq(bo.getType() != null, News::getType, bo.getType());
        lqw.eq(bo.getChannelId() != null, News::getChannelId, bo.getChannelId());
        lqw.eq(StringUtils.isNotBlank(bo.getLabels()), News::getLabels, bo.getLabels());
        lqw.eq(bo.getSubmitedTime() != null, News::getSubmitedTime, bo.getSubmitedTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), News::getStatus, bo.getStatus());
        lqw.eq(bo.getPublishTime() != null, News::getPublishTime, bo.getPublishTime());
        lqw.eq(StringUtils.isNotBlank(bo.getReason()), News::getReason, bo.getReason());
        lqw.eq(StringUtils.isNotBlank(bo.getArticleId()), News::getArticleId, bo.getArticleId());
        lqw.eq(StringUtils.isNotBlank(bo.getImages()), News::getImages, bo.getImages());
        lqw.eq(StringUtils.isNotBlank(bo.getEnable()), News::getEnable, bo.getEnable());
        return lqw;
    }

    /**
     * 新增文章列表
     */
    @Override
    public Boolean insertByBo(NewsBo bo) {
        News add = BeanUtil.toBean(bo, News.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文章列表
     */
    @Override
    public Boolean updateByBo(NewsBo bo) {
        News update = BeanUtil.toBean(bo, News.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(News entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文章列表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
